<template>
    <div class="back">
        <div class="login">
            <h3>叩丁狼商品管理系统</h3>
            <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" type="string" placeholder="请输入用户名"
                        @keyup.enter="submitForm(ruleFormRef)">
                        <template #prefix>
                            <el-icon class="el-input__icon" :size="20">
                                <User />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="ruleForm.password" type="password" placeholder="请输入密码"
                        @keyup.enter="submitForm(ruleFormRef)">
                        <template #prefix>
                            <el-icon class="el-input__icon" :size="20">
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>

</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { User, Lock } from "@element-plus/icons-vue";
import { ElMessage } from 'element-plus'
import { loginAPI } from "./api/index"
import { useRouter } from 'vue-router';

const ruleFormRef = ref<FormInstance>()

const router = useRouter();

const validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请输入密码'))
    } else {
        // 校验成功，就调用callback()
        callback()
    }
}

const ruleForm = reactive({
    username: '',
    password: '',

})

const rules = reactive({
    username: [{ required: true, message: "请输入用户名", trigger: 'blur' }],
    password: [{ validator: validatePass, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            // console.log('submit!')
            loginAPI(ruleForm).then((res) => {
                console.log(res);
                if (res.code === 200) {
                    // 1.存token
                    localStorage.setItem("token", res.data.tokenHead + res.data.token)
                    // 2.提示
                    ElMessage.success("登录成功")
                    // 3.跳转
                    router.push("/index")
                }

            })
        } else {
            ElMessage.error('请输入用户名密码')
            // console.log('error submit!')
            return false
        }
    })
}

</script>

<style scoped lang="less">
.back {
    width: 100%;
    height: 100%;
    display: flex;
    background-color: #2d3a4b;

    .login {
        width: 400px;
        margin: auto;
        text-align: center;

        h3 {
            margin-bottom: 20px;
            color: #fff;
        }

        :deep(.el-input__inner) {
            height: 50px;
            color: #fff;

        }

        :deep(.el-input__wrapper) {
            background-color: transparent;
        }

        :deep(.el-button--primary) {
            width: 100%;
        }
    }
}
</style>